//
//  SWGalleryLayout.swift
//  UIcomnentTest2
//
//  Created by linhan on 2016/12/7.
//  Copyright © 2016年 test. All rights reserved.
//

import Foundation
import UIKit
class SWGalleryLayout: UICollectionViewFlowLayout
{
    //static let ItemWH:CGFloat  = 100;
    
    /** 有效距离:当item的中间x距离屏幕的中间x在HMActiveDistance以内,才会开始放大, 其它情况都是缩小 */
    private let ActiveDistance:CGFloat = 150.0
    /** 缩放因素: 值越大, item就会越大 */
    private let ScaleFactor:CGFloat = 0.2
    
    /**
     *  只要显示的边界发生改变就重新布局:
     内部会重新调用prepareLayout和layoutAttributesForElementsInRect方法获得所有cell的布局属性
     */
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool
    {
        return true
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint
    {
        //print("===targetContentOffset")
        guard let collectionView = collectionView else{
            return CGPoint.zero
        }
        
        let collectionViewFrame = collectionView.frame
        
        // 1.计算出scrollView最后会停留的范围
        let lastRect:CGRect = CGRect(origin: proposedContentOffset, size: collectionViewFrame.size)
        
        // 计算屏幕最中间的x
        let centerX:CGFloat  = proposedContentOffset.x + collectionViewFrame.size.width * 0.5
        
        // 2.取出这个范围内的所有属性
        var adjustOffsetX:CGFloat = CGFloat(Int.max)
        if let attributes = layoutAttributesForElements(in: lastRect)
        {
            for attrs in attributes
            {
                if (abs(attrs.center.x - centerX) < abs(adjustOffsetX)) {
                    adjustOffsetX = attrs.center.x - centerX;
                }
            }
            
        }
        
        return CGPointMake(proposedContentOffset.x + adjustOffsetX, proposedContentOffset.y)
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
    {
        guard let collectionView = collectionView else{
            return nil
        }
        // 0.计算可见的矩形框
        let visiableRect:CGRect = CGRect(origin: collectionView.contentOffset, size: collectionView.frame.size)
        
        // 1.取得默认的cell的UICollectionViewLayoutAttributes
        let array:[UICollectionViewLayoutAttributes]? = super.layoutAttributesForElements(in: rect)?.map({$0.copy()}) as? [UICollectionViewLayoutAttributes]
        if let array = array
        {
            //计算屏幕最中间的x
            let centerX:CGFloat = collectionView.contentOffset.x + collectionView.frame.size.width * 0.5
            
            // 2.遍历所有的布局属性
            for attrs in array
            {
                // 如果不在屏幕上,直接跳过
                if !visiableRect.intersects(attrs.frame)
                {
                    continue
                }
                
                // 每一个item的中点x
                let itemCenterX = attrs.center.x
                // 差距越小, 缩放比例越大
                // 根据跟屏幕最中间的距离计算缩放比例
                let scale:CGFloat = 0.8 + ScaleFactor * (1 - (abs(itemCenterX - centerX) / ActiveDistance))
                attrs.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
        }
        
        
        return array;
    }
    
    
    
}
